package com.core.cloudbusiness.sheet.process.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.core.cloudbusiness.sheet.mayor.service.CloudSheetMayorService;
import com.core.cloudbusiness.sheet.process.mapper.CloudSheetProcessMapper;
import com.core.cloudbusiness.sheet.process.service.CloudSheetProcessService;
import com.core.cloudbusiness.system.user.service.CloudSystemUserService;
import com.core.cloudcommon.CommonCustomException;
import com.core.cloudcommon.CommonInteger;
import com.core.cloudbusiness.tool.CommonRequestContext;
import com.core.cloudcommon.redis.service.CloudRedisTemplateService;
import com.core.cloudcommon.redis.vo.CloudRedisVO;
import com.core.cloundbusinessapi.sheet.mayor.enums.CloudSheetMayorResultCode;
import com.core.cloundbusinessapi.sheet.process.dto.CloudSheetNotSignForDTO;
import com.core.cloundbusinessapi.sheet.process.dto.CloudSheetProcessDTO;
import com.core.cloundbusinessapi.sheet.process.enums.CloudSheetProcessResultCode;
import com.core.cloundbusinessapi.sheet.process.enums.CloudSheetProcessStateEnums;
import com.core.cloundbusinessapi.sheet.process.vo.CloudSheetProcessVO;
import com.core.cloundbusinessapi.sheet.process.vo.CloudSheetQueryProcessListVO;
import com.core.cloundbusinessapi.system.user.dto.CloudSystemUserDTO;
import com.core.cloundbusinessapi.system.user.vo.CloudSystemQueryListUserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author =====Lqj=====
 * @className CloudSheetProcessServiceImpl
 * @description
 * @create 2024/11/27 21:19
 **/
@Service
public class CloudSheetProcessServiceImpl implements CloudSheetProcessService {

    @Autowired
    private CloudSheetProcessMapper cloudSheetProcessMapper;

    @Autowired
    @Lazy
    private CloudSheetMayorService cloudSheetMayorService;

    @Autowired
    private CloudRedisTemplateService cloudRedisTemplateService;


    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    private Snowflake snowflake = IdUtil.createSnowflake(2L, 2L);

    final String notSignForKey = "NotSignFor60000";

    final Long cacheTime = 600L;

    @Override
    public CloudSheetProcessDTO task(CloudSheetProcessVO cloudSheetProcessVO) {
        if (Objects.isNull(cloudSheetProcessVO)) {
            throw new CommonCustomException(CloudSheetProcessResultCode.CODE600001.getCode(), CloudSheetProcessResultCode.CODE600001.getMsg());
        }
        if (cloudSheetProcessVO.getSheetCode() == null) {
            throw new CommonCustomException(CloudSheetProcessResultCode.CODE600002.getCode(), CloudSheetProcessResultCode.CODE600002.getMsg());
        }
        CloudSystemUserDTO user = CommonRequestContext.getCurrentUser();
        String sheetProcessState = cloudSheetProcessVO.getSheetProcessState();
        Integer number = cloudSheetProcessMapper.queryProcessSignMapper(cloudSheetProcessVO.getSheetCode(), CloudSheetProcessStateEnums.getCode(sheetProcessState));
        if (CommonInteger.Integer0 < number) {
            throw new CommonCustomException(CloudSheetProcessResultCode.CODE600006.getCode(), CloudSheetProcessResultCode.CODE600006.getMsg());

        }
        CloudSheetProcessDTO cloudSheetProcessDTO = new CloudSheetProcessDTO();
        BeanUtils.copyProperties(cloudSheetProcessVO, cloudSheetProcessDTO);
        Long id = snowflake.nextId();
        cloudSheetProcessDTO.setId(id);
        cloudSheetProcessDTO.setProcessId(IdUtil.randomUUID());
        cloudSheetProcessDTO.setProcessType(CloudSheetProcessStateEnums.getCode(sheetProcessState));
        cloudSheetProcessDTO.setHandleBy(user.getUserName());
        if (CloudSheetProcessStateEnums.TYPE20.getState().equals(sheetProcessState)) {
            cloudSheetProcessDTO.setHandleOpinion("签收单位：" + cloudSheetProcessVO.getHandleUnit() +
                    "\n签收时间：" + format.format(new Date()));
        }
        cloudSheetProcessMapper.insert(cloudSheetProcessDTO);
        cloudSheetMayorService.updateSheetStatus(cloudSheetProcessVO.getSheetCode(), sheetProcessState, cloudSheetProcessVO.getHandleUnit());
        return cloudSheetProcessDTO;
    }

    @Override
    public List<CloudSheetProcessDTO> queryProcessList(CloudSheetQueryProcessListVO cloudSheetQueryProcessListVO) {
        if (cloudSheetQueryProcessListVO.getSheetCode() == null) {
            throw new CommonCustomException(CloudSheetProcessResultCode.CODE600003.getCode(), CloudSheetProcessResultCode.CODE600003.getMsg());
        }
        List<CloudSheetProcessDTO> cloudSheetProcessDTOS = cloudSheetProcessMapper.queryProcessListMapper(cloudSheetQueryProcessListVO.getSheetCode());
        return cloudSheetProcessDTOS;
    }

    @Override
    public String signFor(String sheetCode) {
        if (sheetCode == null) {
            throw new CommonCustomException(CloudSheetProcessResultCode.CODE600004.getCode(), CloudSheetProcessResultCode.CODE600004.getMsg());
        }
        Integer number = cloudSheetProcessMapper.queryProcessSignMapper(sheetCode, CloudSheetProcessStateEnums.TYPE20.getCode());
        if (CommonInteger.Integer0 < number) {
            throw new CommonCustomException(CloudSheetProcessResultCode.CODE600005.getCode(), CloudSheetProcessResultCode.CODE600005.getMsg());
        }
        CloudSystemUserDTO user = CommonRequestContext.getCurrentUser();
        CloudSheetProcessDTO cloudSheetProcessDTO = new CloudSheetProcessDTO();
        Long id = snowflake.nextId();
        cloudSheetProcessDTO.setId(id);
        cloudSheetProcessDTO.setSheetCode(sheetCode);
        cloudSheetProcessDTO.setProcessId(IdUtil.randomUUID());
        cloudSheetProcessDTO.setProcessType(CloudSheetProcessStateEnums.TYPE20.getCode());
        cloudSheetProcessDTO.setHandleUnit(user.getDepartId().toString());
        cloudSheetProcessDTO.setHandleBy(user.getUserName());
        cloudSheetProcessDTO.setHandleOpinion("签收单位：" + user.getOrgName() +
                "\n签收时间：" + format.format(new Date()));
        cloudSheetProcessDTO.setSheetProcessState(CloudSheetProcessStateEnums.TYPE20.getState());
        cloudSheetProcessMapper.insert(cloudSheetProcessDTO);
        cloudSheetMayorService.updateSheetStatus(sheetCode, CloudSheetProcessStateEnums.TYPE20.getState(), user.getDepartId().toString());
        cloudRedisTemplateService.del(notSignForKey + user.getDepartId());
        return "签收成功";
    }

    /**
     * 查询待签收接口
     *
     * @return
     */
    @Override
    public List<CloudSheetNotSignForDTO> sheetNotSignFor() {
        CloudSystemUserDTO user = CommonRequestContext.getCurrentUser();
        String departId = user.getDepartId().toString();
        Object o = cloudRedisTemplateService.get(new CloudRedisVO(notSignForKey + departId));
        if (Objects.nonNull(o)) {
            List<CloudSheetNotSignForDTO> cloudSheetNotSignForDTOS = JSON.parseArray(o.toString(), CloudSheetNotSignForDTO.class);
            return cloudSheetNotSignForDTOS;
        }
        List<CloudSheetNotSignForDTO> cloudSheetNotSignForDTOS = cloudSheetProcessMapper.sheetNotSignFor(user.getDepartId().toString());
        cloudRedisTemplateService.set(new CloudRedisVO(notSignForKey + departId, JSON.toJSONString(cloudSheetNotSignForDTOS), cacheTime));
        return cloudSheetNotSignForDTOS;
    }
}
